package com.mvc4g.example.client;

import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.SourcesTabEvents;
import com.google.gwt.user.client.ui.TabBar;
import com.google.gwt.user.client.ui.TabListener;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.mvc4g.client.Controller;
import com.mvc4g.client.Event;
import com.mvc4g.client.ViewInterface;

public class MainView extends Composite implements ViewInterface {

	private SimplePanel body = new SimplePanel();
	private Controller controller = null;
	
	public MainView(){
		VerticalPanel mainPanel = new VerticalPanel();
		
		mainPanel.add(initMenu());
		body.setPixelSize(300, 100);
		mainPanel.add(body);
		
		RootPanel.get().add(mainPanel);
	}

	public void setController(Controller controller) {
		this.controller = controller;
	}

	public void changePage(Widget page) {
		body.clear();
		page.setVisible(true);
		body.add(page);
	}

	private TabBar initMenu() {
		TabBar menu = new TabBar();
		menu.addTab("Page 1");
		menu.addTab("Page 2");
		menu.addTab("Page 3");

		menu.addTabListener(new TabListener() {

			public boolean onBeforeTabSelected(SourcesTabEvents sender,
					int tabIndex) {
				return true;
			}

			public void onTabSelected(SourcesTabEvents sender, int tabIndex) {
				tabIndex++;
				controller.handleEvent(new Event("changePage", "Page" + tabIndex ));
			}

		});

		return menu;
	}

}
